package com.platform.jpa.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class ArrayUtil {
    public static String toString(List<Integer> datas, String split) {
        return listToString(datas, split);
    }

    public static <T> String toString(T[] datas, String split) {
        return listToString(Arrays.asList(datas), split);
    }

    public static <T> String listToString(List<T> datas, String split) {
        if (datas.isEmpty())
            return "";
        StringBuilder sb = new StringBuilder().append(datas.get(0));

        for (int i = 1; i < datas.size(); i++) {
            sb.append(split).append(datas.get(i));
        }

        return sb.toString();
    }

    public static <T> List<Long> getListIds(List<T> datas, String pro) {
        if (datas.isEmpty())
            return null;
        List<Long> list = new ArrayList<>();
        for (T t : datas) {
            Long id = (Long) ReflectUtil.getProperty(t, pro);
            list.add(id);
        }
        return list;
    }

    /**
     * 生成一组随机数
     *
     * @param max    最大值
     * @param lenght 个数
     * @return
     */
    public static List<Long> getRandomNum(long max, int lenght) {
        List<Long> nums = new ArrayList<>();
        Random random = new Random(max);
        for (int i = 0; i < lenght; i++)
            nums.add(random.nextLong());
        return nums;
    }
}
